package selectClass.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import selectClass.Student;
import selectClass.Taco;
import selectClass.data.StudentRepository;

import javax.validation.Valid;
import java.io.*;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/studentInformation")
@SessionAttributes("taco")
public class studentInformationController {
    private StudentRepository studentRepository;

    @Autowired
    public studentInformationController(StudentRepository studentRepository) {
        this.studentRepository = studentRepository;
    }

    @ModelAttribute(name = "taco")
    public Taco taco(){
        return new Taco();
    }

    @ModelAttribute(name = "student")
    public Student student(){return new Student();}

    @GetMapping("/current")
    public String studentInformation(Model model) {
        model.addAttribute("student", new Student());
        return "studentInformation";
    }

    @PostMapping
    public String processOrder(@Valid Student student, Errors errors,@ModelAttribute Taco taco) {
        if (errors.hasErrors()) {
            return "studentInformation";
    }

        Student saved=studentRepository.save(student);
        taco.addDesign(saved);

        return "redirect:/design";
    }
}
